<template>
  <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
    <el-tab-pane :label="$t('menu.profile')" name="info">
      <el-form :model="formData" label-width="80px" ref="formRef" :rules="rules">
        <el-form-item label="用户名">
          <el-input v-model="formData.name" disabled />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.alias" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="formData.department" />
        </el-form-item>
        <el-form-item label="角色">
          <el-tag v-for="role in formData.roles" :key="role" type="primary" style="margin-right: 5px;">
            {{ role.alias }}
          </el-tag>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-text type="info">{{ formData.created_at }}</el-text>
        </el-form-item>
        <el-button type="primary" @click="saveUserInfo" :loading="saving">保存</el-button>
      </el-form>
    </el-tab-pane>

    <el-tab-pane :label="$t('menu.authcode')" name="apikey">
      <el-button type="primary" @click="apiKeyVisible = true">创建授权</el-button>
      <el-table :data="formAuthCode.authCode" style="width: 100%">
        <el-table-column prop="apikey" label="授权码" width="180" />
        <el-table-column prop="created_at" label="授权时间" width="180" />
        <el-table-column prop="expired_at" label="过期时间" />
      </el-table>
      <ApiKey v-model="apiKeyVisible"></ApiKey>
    </el-tab-pane>

    <el-tab-pane :label="$t('menu.changepassword')" name="changPassword">
      <el-form :model="formData" label-width="80px">
        <el-form-item label="原密码" prop="name">
          <el-input v-model="formData.alias" />
        </el-form-item>
        <el-form-item label="密码" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="确认密码" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="saveUserInfo" :loading="saving">保存</el-button>
    </el-tab-pane>

  </el-tabs>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import type { TabsPaneContext } from 'element-plus'
import ApiKey from '../../components/Popup/Owner/ApiKey.vue'

const activeName = ref('info')
const apiKeyVisible = ref(false)
const userStore = useUserStore()
const formRef = ref()
const saving = ref(false)

const formData = ref({
  alias: '',
  name: '',
  email: '',
  phone: '',
  department: '',
  roles: [],
  created_at: ''
})

const formAuthCode = ref({
  authCode: []
})

const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event)
}

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号码', trigger: 'blur' }
  ]
}

// 保存用户信息
const saveUserInfo = async () => {
  try {
    // 表单验证
    const valid = await formRef.value.validate()
    if (!valid) return

    saving.value = true

    // 调用store更新用户信息
    await userStore.updateUserInfo(formData.value)

    ElMessage.success('个人信息保存成功')
  } catch (error) {
    ElMessage.error('保存失败，请重试')
    console.error('Save user info error:', error)
  } finally {
    saving.value = false
  }
}

onMounted(() => {
  formData.value = userStore.userInfo
})
</script>

<style></style>
